//
//  VerifyAccountVC.m
//  QGSPos
//
//  Created by vinh pham on 6/25/13.
//  Copyright (c) 2013 QGS Inc. All rights reserved.
//

#import "VerifyAccountVC.h"
#import "ListServerVCNew.h"

@interface VerifyAccountVC ()

@end

@implementation VerifyAccountVC
@synthesize parent;
@synthesize txtEmail,txtPassword;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil parent:(id)_parent
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        parent = _parent;
    }
    return self;
}

- (IBAction) cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction) confirm:(id)sender {
    if (txtEmail.text == nil || [txtEmail.text isEqualToString:@""]) {
        UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please input your email." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [dialog show];
        return;
    }
    if (txtPassword.text == nil || [txtEmail.text isEqualToString:@""]) {
        UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Please input your password." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [dialog show];
        return;
    }

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[FMSLib shareClient] checkPermissionUser:txtEmail.text auth:txtPassword.text success:^(NSMutableDictionary *response){
        if ([[response objectForKey:@"status"] intValue] == 1)
        {
            [(ListServerVCNew*)parent deleteLocalDatabase];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else {
            UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Your account doesn't have permission to delete this database." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [dialog show];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
    }failure:^(NSError *error){
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Your account doesn't have permission to delete this database." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [dialog show];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });
    }];
    
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

@end
